package com.uibase.card
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import com.uibase.card.func.ICardClickControl;
	
	public class Card extends Sprite
	{
		private var _val:int;
		private var _mcCard:Sprite;
		public var index:int;
		private var _selected:Boolean;
		
		private var _cardClickCtrl:ICardClickControl;
		private var _cardRotation:int;
		
		static public const CARD_SELECTED_Y_POS:int = 20;
		
		private var _cardShowStat:int;  //牌展示 0-正面 1-反面
		
		public function Card(val:int)
		{
			super();
			_val = val;
			_mcCard = CardPool.getCardByVal(_val);
			if(_mcCard)
			{
				_cardShowStat = 0;
				addChild(_mcCard);
				_mcCard.x = -CardDefine.CARD_WIDTH/2;
			}
		}
		
		public function set cardClickControl(iCardClick:ICardClickControl):void
		{
			if(_cardClickCtrl)
			{
				_cardClickCtrl.onCardDeattach();
				_cardClickCtrl = null;
			}
			_cardClickCtrl = iCardClick;
			if(!_cardClickCtrl)
			{
				removeEventListener(MouseEvent.CLICK, onClick);
			}
			else
			{
				_cardClickCtrl.onCardAttach(this);
				addEventListener(MouseEvent.CLICK, onClick);
			}
		}
		
		public function get cardClickControl():ICardClickControl
		{
			return _cardClickCtrl;
		}
		
		private function onClick(e:MouseEvent):void
		{
			if(_cardClickCtrl)
			{
				_cardClickCtrl.onCardClick();
			}
		}
		
		public function get color():int
		{
			if(_val<CardDefine.CARD_VAL_MIN || _val>CardDefine.CARD_VAL_MAX)
			{
				return CardDefine.COLOR_NONE;
			}
			
			return _val/CardDefine.EACH_COLOR_NUM;
		}
		
		public function get showVal():int
		{
			if(_val<CardDefine.CARD_VAL_MIN || _val>CardDefine.CARD_VAL_MAX)
			{
				return CardDefine.CARD_VAL_NONE;
			}
			
			return _val%CardDefine.EACH_COLOR_NUM;
		}
		
		public function get val():int
		{
			return _val;
		}
		
		public function set cardRotation(rotationVal:int):void
		{
			_cardRotation = rotationVal%360;
			if((_cardRotation>0 && _cardRotation<90)
				||(_cardRotation>270&&_cardRotation<360))
			{
				if(_cardShowStat==1 && _mcCard)
				{
					_cardShowStat = 0;
					if(_mcCard)
					{
						removeChild(_mcCard);
						_mcCard = null;
					}
					_mcCard = CardPool.getCardByVal(val);
					if(_mcCard)
					{
						addChild(_mcCard);
						_mcCard.x = -CardDefine.CARD_WIDTH/2;
					}
				}
				
				rotationY = _cardRotation;
			}
			
			if(_cardRotation>=90 && _cardRotation<=270)
			{
				if(_cardShowStat==0 && _mcCard)
				{
					_cardShowStat = 1;
					if(_mcCard)
					{
						removeChild(_mcCard);
						_mcCard = null;
					}
					_mcCard = CardPool.getCardByVal(CardDefine.CARD_BACK);
					if(_mcCard)
					{
						addChild(_mcCard);
						_mcCard.x = -CardDefine.CARD_WIDTH/2;
					}
				}
				
				rotationY = _cardRotation+180;
			}
		}
		
		public function get cardRotation():int
		{
			return _cardRotation;
		}
		
		public function destroy():void
		{
			cardClickControl = null;
		}
	}
}